package com.lq.javawebTest.utils;

import com.lq.javawebTest.core.entity.HttpRequest;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class IOUtils {
    // 读取流中的数据，并转化为String对象
    public static String readString(InputStream inputStream){

        try {
            int len;
            byte[] buffer = new byte[1024];
            //从流当中读取请求的报文
            StringBuilder sb = new StringBuilder();

            //这里如果写成while循环会出错，把空指针异常，因为存在客户端还没有把内容发过来
            //然后服务器太快了，拿着空的东西就走了
            do{
                len = inputStream.read(buffer);//阻塞的方法一直堵在这里读，没有就退出
                sb.append(new String(buffer,0,len));
            }while (inputStream.available() > 0);

            return sb.toString();
        }catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }

    //根据流转化的字符串截串，实例化报文对象
    public static HttpRequest getRequest(HttpRequest Httprequest, String requestMessage){
        // 通过大量的截串获取对应信息
        String[] headerAndBody = requestMessage.split("\r\n\r\n");

        // 判断有没有请求体，并且请求体不为空
        if(headerAndBody.length > 1 && headerAndBody[1] != null){
            Httprequest.setBody(headerAndBody[1]);
        }
        // 将请求行和首部信息截取
        String[] lineAndHeader = headerAndBody[0].split("\r\n");
        String line = lineAndHeader[0];

        // 使用空格截取请求行信息
        String[] lines = line.split(" ");
        Httprequest.setType(lines[0]);

        //处理uri,可能有参数    /user?name=zs&id=1
        String[] uriAndParam = lines[1].split("\\?");
        Httprequest.setUri(uriAndParam[0]);
        if(uriAndParam.length > 1){
            String[] params = uriAndParam[1].split("&");
            for (String s : params) {
                String[] param = s.split("=");
                Httprequest.setParameter(param[0], param[1]);
            }
        }

        Httprequest.setProtocol(lines[2]);

        // 除了第一行，其他的都是首部信息
        for (int i = 1; i < lineAndHeader.length; i++) {
            String[] split = lineAndHeader[i].split(": ");
            Httprequest.setHeader(split[0],split[1]);
        }

        //处理响应体，判断content-Type是不是application/x-www-form-urlencoded，如果是就是form的post表单提交
        if("application/x-www-form-urlencoded".equals(Httprequest.getHeader("Content-Type"))){
            String[] params = Httprequest.getBody().split("&");
            for (String s : params) {
                String[] param = s.split("=");
                Httprequest.setParameter(param[0], param[1]);
            }
        }
        return Httprequest;
    }

    //将传入路径中的文件读入到流当中，然后在转化为String实例
    public static String getContext(String path){
        File file = new File(path);
        if(!file.exists()){
            return null;
        }
        try(
                FileInputStream fileInputStream = new FileInputStream(path)
        ){
            StringBuilder stringBuilder = new StringBuilder();
            byte[] buffer = new byte[1024];
            int len;
            while((len = fileInputStream.read(buffer)) != -1){
                stringBuilder.append(new String(buffer,0,len));
            }
            return stringBuilder.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
